const stor = require('../sorage/sqlite3.js');

module.exports = (req, res) => {
  const id = parseInt(req.url.split('/')[2]);
  let body = '';

  req.on('data', chunk => {
    body += chunk;
  });

  req.on('end', async () => {
    const parseBody = new URLSearchParams(body);
    const newData = {};
    parseBody.forEach((value, key) => {
      newData[key] = (key === 'age') ? parseInt(value) : value;
    });
    const updateUser = await stor.updateUser(id, newData);

    if (updateUser) {
      res.writeHead(200);
      res.end(JSON.stringify(updateUser));
    } else {
      res.writeHead(400);
      res.end(JSON.stringify({ message: 'User not found' }));
    }

  });
}